import Tools from '../utils/index.js'
import store from '../store/index.js'
// const baseUrl = "https://dd.salasiac.com";
// const baseUrl = "https://dev-api.midzus.com";
const baseUrl = "https://api.midzus.com";

import moment from 'moment-timezone';

// https://api.midzus.com   https://dev-api.midzus.com


// wx8d0e4747ffd125b5 测试
// wxf35c8fad3b0b354b 正式

export default  function request (path, method, data, timeout = 10000) {
	return new Promise(async function(resolve, reject){
		uni.showNavigationBarLoading()
		await uni.request({
		    url: baseUrl  + path, //仅为示例，并非真实接口地址。  	  
			header: {
				'Accept': 'application/json,text/plain, */*',
				'Content-Type': 'application/json', //自定义请求头信息
				'Authorization': Tools.getStorage('token') ?'Bearer '+ Tools.getStorage('token') : '',
				'request-time': moment().format(),
				// 'Referer': 'https://servicewechat.com/wx63e36dd18172f38d/devtools/page.frame.html',
				//  'referrer-policy': 'unsafe-url' // 设置referrer-policy
			},		
			data, 
			method,
			timeout,		
			success: (res) => {
				if (res.data.status == 'success' && res.data.code == 200) {
					resolve(res.data.data);
				} 
				else if (res.data.code === 401) {
					uni.clearStorage();
					Tools.toast("登录失效,请重新登录 !");
					// #ifdef MP-WEIXIN
					Tools.goPage(1, '/pages/login/index')
					// #endif
				} 
				else{
					Tools.toast(res.data.message);
					
				}
				uni.hideNavigationBarLoading();
				// setTimeout(()=>{
				// 	uni.hideLoading();
				// }, 500);
			},
			fail: (res) => {
				setTimeout(()=>{
					uni.hideLoading();
				}, 500);
				// reject(res.data.message)
				// Tools.toast(res.data.message);
			}
		});
	}) 
}

export function getBaseUrl () {
	return baseUrl
}
	
